package com.chance.cc.crawler.prod.command.job.domain.vm.bitauto.adhoc;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.record.CrawlerRecord;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.vm.bitauto.BitAutoCommonCrawlerSchedulerJob;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.turnPageItem;
import static com.chance.cc.crawler.development.scripts.allfeild.AICCommonField.Tag_Site_Info;

/**
 * @ClassName bitauto
 * @Description TODO
 * @Author songding
 * @Date 2021/9/22 11:17
 * @Version 1.0
 **/
public class BitAutoSimpleRedisToRunCrawlerScheduleJob  extends BitAutoCommonCrawlerSchedulerJob {
    private static final String crawler_level = "simple";
    /**
     * 发布任务信息
     * @return
     */
    public static void main(String[] args) {
        publishAutoHomeCrawlerScheduleJobInfo();
    }
    public static CrawlerJob publishAutoHomeCrawlerScheduleJobInfo(){

        CrawlerJob Scheduler = Article();//文章
        //CrawlerJob Scheduler = video();//文章
        //发布定时任务
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(Scheduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("汽车之家临时采集任务");
        crawlerScheduleJob.setCrawlerKey(Scheduler.generateCrawlerKey());
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布任务：" + httpPage.getRawText());
        return Scheduler;
    }
    //文章
    public static CrawlerJob Article(){
        String site = "article";
        String site_biz = "simple";
        String site_info = "simple-20210922-20211102";
        String redisQueueName = "crawler-bitauto-article-simple-queue";

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, turnPageItem)
                .domain(domain)
                .httpUrl("https://news.yiche.com/xinchexiaoxi/20210918/1215024026.html")
                .releaseTime(System.currentTimeMillis())
                .httpConfig(HttpConfig.me(domain))
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*42,null))
                //.resultLabelTag(CrawlerEnum.CrawlerDataType.article)
               // .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .proxy(proxy)
                .build();
        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);
        requestRecord.tagsCreator().bizTags().addDomain(domain);
        requestRecord.tagsCreator().bizTags().addSite(site);
        requestRecord.tagsCreator().bizTags().addSiteBiz(site_biz);
        requestRecord.tagsCreator().bizTags().addCustomKV(Tag_Site_Info,site_info);


        CrawlerRecord crawlerRecord = new CrawlerRecord();
        crawlerRecord.setFilter(CrawlerEnum.CrawlerRecordFilter.dateRange);
        crawlerRecord.addFilterInfo(FilterUtils.dateRangeFilterInfo(24*42,null));
        requestRecord.tagsCreator().bizTags().addCustomKV("comment_record_filter_info",JSON.toJSONString(crawlerRecord));

        return CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),//bitauto-article-simple-simple-20210930-simple-realtime
                        StringUtils.joinWith("-",site,site_biz,site_info,crawler_level,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(redisQueueName))
                .fileResultPipeline(null,"/data/chance_crawler_runner/logs/node/bitArticle.log",false)
                .kafkaResultPipeline(null,kafkaTopciForTraceJob,null)
                .crawlerJobThreadNumber(10)
                .requestRecord(requestRecord)
                .build();
    }
    //视频
    public static CrawlerJob video(){
        String site = "video";
        String site_biz = "";
        String site_info = "";
        String redisQueueName = "crawler-bitauto-video-simple-queue";

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, turnPageItem)
                .domain(domain)
                .httpUrl("https://chejiahao.autohome.com.cn/?infotype=1")
                .releaseTime(System.currentTimeMillis())
                .httpConfig(HttpConfig.me(domain))
                .filter(CrawlerEnum.CrawlerRecordFilter.dateRange)
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(24*1,null))
                .resultLabelTag(CrawlerEnum.CrawlerDataType.article)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.interaction)
                .resultLabelTag(CrawlerEnum.CrawlerDataType.comment)
                .proxy(proxy)
                .build();
        requestRecord.tagsCreator().bizTags().addDomain(domain);
        requestRecord.tagsCreator().bizTags().addSite(site);
        requestRecord.tagsCreator().bizTags().addSiteBiz(site_biz);
        requestRecord.tagsCreator().bizTags().addCustomKV(Tag_Site_Info,site_info);


        CrawlerRecord crawlerRecord = new CrawlerRecord();
        crawlerRecord.setFilter(CrawlerEnum.CrawlerRecordFilter.dateRange);
        crawlerRecord.addFilterInfo(FilterUtils.dateRangeFilterInfo(24*1,null));
        requestRecord.tagsCreator().bizTags().addCustomKV("comment_record_filter_info",JSON.toJSONString(crawlerRecord));

        return CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",site,site_biz,site_info,crawler_level,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(redisQueueName))
                .kafkaResultPipeline(null,kafkaTopciForTraceJob,null)
                .crawlerJobThreadNumber(10)
                .requestRecord(requestRecord)
                .build();
    }
}
